import { Component, OnInit, Input, ViewChild } from '@angular/core';
import { NzModalService, NzMessageService } from 'ng-zorro-antd';
import { UserService } from 'src/app/core/service/user.service';
import { AddOrgUserComponent } from './add-org-user/add-org-user.component';
import { EditOrgUserComponent } from './edit-org-user/edit-org-user.component';
import { EditAdminUserComponent } from './edit-admin-user/edit-admin-user.component';

@Component({
  selector: 'app-org-users',
  templateUrl: './org-users.component.html',
  styleUrls: ['./org-users.component.scss']
})
export class OrgUsersComponent implements OnInit {

  allChecked = false;
  indeterminate = false;
  displayData = [];
  optionList: any;
  groupList: any;
  roleGroup: any;
  isVisible = false;
  isOkLoading = false;
  filter: string;
  isWarn = true;

  // 表格显示样式（页码、每页显示多少页、总共几页、获取到的数据）
  pageIndex = 1;      // 页码标识
  pageSize = 10;      // 每页展示多少数据 —— 10条
  total = 1;          // 总共1页
  dataSet = [];       // 数据集合
  loading = true;     // 加载
  sortValue = null;   // 种类值
  sortKey = null;     // 种类关键值
  searchGenderList: string[] = [];

  info = {};

  constructor(
    private userService: UserService,
    private modalService: NzModalService,
    private nzMessageService: NzMessageService
  ) { }

  /**
   * 获取企业用户列表
   * @param reset 重置
   */
  getSuperUsers(reset: boolean = false): void {
    if (reset) {
      this.pageIndex = 1;
    }
    this.userService.getSuperUsers(this.pageIndex, this.pageSize, this.filter)
      .subscribe((superUsers) => {
        console.log(`===superUsers=json数据===> ${superUsers}`);
        this.loading = false;                 // 获取到数据之后，不再显示加载图标
        this.total = superUsers.total;        // 数据的条数
        this.dataSet = superUsers.data;       // 数据集
        console.log('===superUsers现在的数据===', superUsers.data);
      });
  }

  cancel(): void {
    this.nzMessageService.info('取消删除!');
  }

  /**
   * 执行删除企业用户
   */
  delete(organizationUuid: string, userUuid: string): void {
    this.info = {
      orgUuid: organizationUuid,
      userUuid: userUuid
    };
    this.userService.deleteOrgUser(this.info).subscribe(deleteData => {
      if (deleteData.status === 1) {
        this.nzMessageService.success('删除成功!');
        this.getSuperUsers();
      }
    });
  }

  currentPageDataChange($event: Array<{ name: string; age: number; address: string; checked: boolean; disabled: boolean; }>): void {
    this.displayData = $event;
    this.refreshStatus();
  }

  /**
   * 刷新状态
   */
  refreshStatus(): void {
    const allChecked = this.displayData.filter(value => !value.disabled).every(value => value.checked === true);
    const allUnChecked = this.displayData.filter(value => !value.disabled).every(value => !value.checked);
    this.allChecked = allChecked;
    this.indeterminate = (!allChecked) && (!allUnChecked);
  }

  ngOnInit(): void {
    this.getSuperUsers();
  }

  /**
   * 创建用户的模态框
   */
  createOrgUserModal(): void {
    const modal = this.modalService.create({
      nzTitle: '创建用户',
      nzContent: AddOrgUserComponent,
      nzComponentParams: {
      },
      nzFooter: null
    });
    modal.afterOpen.subscribe(() => console.log('[afterOpen] emitted!'));
    modal.afterClose.subscribe(result => {
      this.getSuperUsers();
    });
  }

  /**
   * 编辑企业用户
   */
  editOrgUserModal(orgName: string, orgUuid: string): void {
    const modal = this.modalService.create({
      nzTitle: '修改企业',
      nzContent: EditOrgUserComponent,
      nzComponentParams: {
        orgName: orgName,
        orgUuid: orgUuid
      },
      nzFooter: null
    });
    modal.afterOpen.subscribe(() => console.log('[afterOpen] emitted!'));
    modal.afterClose.subscribe(result => {
      this.getSuperUsers();
    });
  }


  /**
   * 编辑企业用户
   */
  editAdminUserModal(email: string, firstName: string, lastName: string, userUuid: string): void {
    const modal = this.modalService.create({
      nzTitle: '修改管理员',
      nzContent: EditAdminUserComponent,
      nzComponentParams: {
        email: email,
        firstName: firstName,
        lastName: lastName,
        userUuid: userUuid
      },
      nzFooter: null
    });
    modal.afterOpen.subscribe(() => console.log('[afterOpen] emitted!'));
    modal.afterClose.subscribe(result => {
      this.getSuperUsers();
    });
  }
}
